package com.zhourh.bang.modular.edu.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.zhourh.bang.common.controller.BaseController;
import com.zhourh.bang.common.exception.BizExceptionEnum;
import com.zhourh.bang.common.exception.BussinessException;
import com.zhourh.bang.common.persistence.dao.SStudentMapper;
import com.zhourh.bang.common.persistence.model.SStudent;
import com.zhourh.bang.core.util.ToolUtil;
import com.zhourh.bang.modular.edu.dao.StudentDao;
import com.zhourh.bang.modular.edu.service.IStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 学生控制器
 *
 * @author fengshuonan
 * @Date 2018-06-22 10:54:13
 */
@Controller
@RequestMapping("/student")
public class StudentController extends BaseController {

    private String PREFIX = "/edu/student/";

    @Resource
    SStudentMapper studentMapper;

    @Resource
    StudentDao studentDao;

    @Resource
    IStudentService studentService;

    /**
     * 跳转到学生首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "student.html";
    }

    /**
     * 跳转到添加学生
     */
    @RequestMapping("/student_add")
    public String studentAdd() {
        return PREFIX + "student_add.html";
    }

    /**
     * 跳转到修改学生
     */
    @RequestMapping("/student_update/{studentId}")
    public String studentUpdate(@PathVariable Integer studentId, Model model) {
        SStudent student = this.studentMapper.selectById(studentId);
        model.addAttribute("student",student);
        return PREFIX + "student_edit.html";
    }

    /**
     * 获取学生列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return this.studentDao.list(condition);
    }

    /**
     * 新增学生
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SStudent student) {
        if(ToolUtil.isOneEmpty(student,student.getName(),student.getId())){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.studentMapper.insert(student);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除学生
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer studentId) {
        return this.studentMapper.deleteById(studentId);
    }


    /**
     * 修改学生
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SStudent student) {
        if(ToolUtil.isOneEmpty(student,student.getName(),student.getId())){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.studentMapper.updateById(student);
        return super.SUCCESS_TIP;
    }

    /**
     * 学生详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(String studentId) {
        return this.studentMapper.selectById(studentId);
    }

    /**
     * 上传学生列表
     */
    @RequestMapping(value = "upload")
    @ResponseBody
    public Object upload(MultipartFile file){
        System.out.println(file.getName());
        System.out.println(file.getSize());
        System.out.println(file.getOriginalFilename());
        int count = 0;
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<SStudent> studentList = reader.readAll(SStudent.class);
            if(studentList!=null && !studentList.isEmpty()){
                for(SStudent student :studentList){
                    if(student.insert()){
                        count++;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return count;
    }
}
